import {observable, action, toJS,computed} from 'mobx';
import {message} from 'antd';
import * as api from '../../apis/plan/planAuditListApi';
import {WeaTableNew} from 'comsMobx';
const {TableStore} = WeaTableNew;
import { WeaLocaleProvider,WeaTools} from 'ecCom';
import {idsAndNamesToArr,getWeekOfYear} from '../../util/Util'
const getLabel = WeaLocaleProvider.getLabel;
export default class PlanAuditListSotre {
  //带observable 的会重新渲染

  @observable loading = true; // 页面初始化的loading状态：数据加载成功前后前使用
  //   @observable hasRight = false; // 判断用户是有权限查看当前页面： 没有权限渲染无权限页面，有权限渲染数据
  @observable tableStore = new TableStore (); // new table

  @observable dateTypeOptions=[]
  // @observable searchConditionItems = []

  @observable noAuditCount=0

  @observable data = {
    searchConditionItems: [],
    searchHumSelectValue: '',
    searchHumNameSelectValue: '',
    searchIncludeSubSelectValue: '3',
    searchIncludeDepSelectValue: '3',
    searchDepSelectValue: '',
    searchSubSelectValue: '',
    searchDateType: '1',
    searchTitleValue: '',
    operation: '',
    planIds: [],
  };


  @computed  get hrmData() { //人力资源回显数据
    return idsAndNamesToArr(this.data.searchHumSelectValue, this.data.searchHumNameSelectValue);
  };




  @action ('设置属性')
  setData = (value, type, callback) => {
    this.data[type] = value;
    if (callback) {
      setTimeout (() => {
        callback ();
      }, 50);
    }
  };

  @action ('获取data')
  getData = () => {
    return this.data;
  };

  @action ('重置')
  resetSearchConditions = () => {
    this.data = {
      searchConditionItems: [],
      searchHumSelectValue: [],
      searchIncludeSubSelectValue: '3',
      searchIncludeDepSelectValue: '3',
      searchDepSelectValue: [],
      searchSubSelectValue: [],
      searchTitleValue: '',
      searchDateType: '',
    };
  };

  //获取表格的元数据
  @action ('获取表格的元数据')
  getAuditListTable = params => {
    api.getAuditListTable (this.getRequestParams ()).then (
      action (res => {
        if (res.api_status) {
          if(params &&params.refresh){
              this.tableStore.getDatas(res.datas,1)
          }else{
              this.tableStore.getDatas(res.datas)
          }
          this.loading = false;
        } else {
          message.error (res.msg || getLabel('383746','请求失败') );
        }
      })
    );
  };
  //审批操作
  @action ('审批操作')
  auditOperation = (doback) => {
    const d = this.data;
    let _this = this;
    api.auditOperation (this.getRequestParams ()).then (
      action (res => {
        if (res.api_status) {
          _this.dealAuditResult (res);
          _this.getAuditListTable ();
          if(doback){
            doback()
          }
        } else {
          message.error (res.msg || getLabel('383746','请求失败') );
        }
      })
    );
  };

  //单个审批操作
  @action ('单个审批操作')
  singleAuditOperation = (planid, operation,doback) => {
    const d = this.data;
    let _this = this;
    api.auditPlan ({planid, operation}).then (
      action (res => {
        if (res.api_status) {
          message.success (getLabel('30700','操作成功') );
          _this.getAuditListTable ();
          if(doback){
            doback()
          }
        } else {
          message.error (res.msg || getLabel('383746','请求失败') );
        }
      })
    );
  };

  //获取日期类型
  @action('getDateType')
  getOption = ()=>{
      WeaTools.callApi('/api/workrelate/plan/getOption', 'GET', {type:1}).then(
        action(res=>{
          if(res.api_status){
            this.dateTypeOptions=res.data
          }else{
            message.error (res.msg || getLabel('383746','请求失败') );
          }
        })
      )
  }

  //处理返回信息
  dealAuditResult = res => {
    if (res.totalcount == 0) {
      message.info (
        this.data.operation == 'all_approve'
          ? getLabel('509096','当前查询条件不存在可以批准的总结计划!') 
          : getLabel('509097','当前查询条件不存在可以退回的总结计划!') 
      );
      return;
    }
    if (res.totalcount) {
      message.success (getLabel('509091','操作成功,共$code$条数据').replace("$code$",res.totalcount));
      return;
    }
    message.success (getLabel('30700','操作成功') );
  };

  //获取后端所需的请求参数
  getRequestParams = () => {
    let d = this.data;
    let params = {
      searchHumSelectValue: d.searchHumSelectValue,
      searchIncludeSubSelectValue: d.searchIncludeSubSelectValue,
      searchIncludeDepSelectValue: d.searchIncludeDepSelectValue,
      searchDepSelectValue: d.searchDepSelectValue,
      searchSubSelectValue: d.searchSubSelectValue,
      searchDateType: d.searchDateType,
      operation: d.operation,
      planIds: d.planIds,
      searchTitleValue: d.searchTitleValue,
    };
    return params;
  };

}
